package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.BathRoomSkuBean;
import com.wnhz.work.yz.bean.OrderInfo;
import com.wnhz.work.yz.bean.SingleItemBean;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddCart2Activity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "AddCart2Activity";
    private TextView shopping_number;
    private ImageView reduce;
    private EditText room_ed;
    private EditText remark_ed;
    private String money;
    private String packageId;
    private String skuId;
    private int nums;
    private DecimalFormat df;
    private String type;

    private TextView tv_all_price;
    private RelativeLayout btn_main_right;
    private String price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cart2);
        initTitle();
        money = getIntent().getStringExtra("money");
        price = getIntent().getStringExtra("price");
        packageId = getIntent().getStringExtra("packageId");
        skuId = getIntent().getStringExtra("skuId");
        type = getIntent().getStringExtra("type");
        nums = getIntent().getIntExtra("nums",1);
        btn_main_right = (RelativeLayout) findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.GONE);
        room_ed = (EditText) findViewById(R.id.room_ed);
        remark_ed = (EditText) findViewById(R.id.remark_ed);
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);
        reduce = (ImageView) findViewById(R.id.reduce);

        reduce.setOnClickListener(this);
        findViewById(R.id.ll_add).setOnClickListener(this);
        findViewById(R.id.tv_add_card).setOnClickListener(this);
        df = new DecimalFormat("######0.00");
     //   tv_all_price.setText("¥ " + df.format(Double.parseDouble(money)));
        shopping_number = (TextView) findViewById(R.id.shopping_number);
        shopping_number.setText(nums+"");
        tv_all_price.setText("￥ " + df.format(Double.parseDouble(price) * nums));
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("结算");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.reduce://减少
                if (nums > 1) {
                    nums--;
                }
                if (nums > 1) {
                    reduce.setImageResource(R.mipmap.ic_decrease_li);
                } else {
                    reduce.setImageResource(R.mipmap.ic_decrease_nor);
                }
                shopping_number.setText(nums + "");
                tv_all_price.setText("￥ " + df.format(Double.parseDouble(price) * nums));
                break;

            case R.id.ll_add://增加
                nums++;
                shopping_number.setText(nums + "");
                tv_all_price.setText("￥ " + df.format(Double.parseDouble(price) * nums));
                break;
            //立即下单
            case R.id.tv_add_card:
                //房间号
                String room_number = room_ed.getText().toString();

                //备注（选填）
                String intro = remark_ed.getText().toString();

                //数量
                nums = Integer.parseInt(shopping_number.getText().toString());

                showDialog();
                Map<String, Object> map = new HashMap<>();
                map.put("token", MyApplication.getInstance().getUserBean().getToken());
                map.put("package_id", packageId);

                List<BathRoomSkuBean.InfoEntity> bathRoomList2 = (List<BathRoomSkuBean.InfoEntity>) getIntent().getSerializableExtra("data");
//                for (int i=0;i<bathRoomList2.size();i++) {
//                    map.put("sku_id", bathRoomList2.get(i).getSku_id());
//                 //   bathRoomList2.get(i).getChooseNum()
//                }
//                StringBuilder skubuilder = new StringBuilder("-");
//                StringBuilder numbuilder = new StringBuilder("-");
//
//
//                for (int i = 0; i < bathRoomList2.size(); i++) {
//                    skubuilder.append(bathRoomList2.get(i).getSku_id());
//                    skubuilder.append("-");
//                    numbuilder.append(bathRoomList2.get(i).getSku_id());
//                    numbuilder.append("-");
//                }
//                if(!intro.equals("")){
//                    map.put("remark", intro);
//                }
                map.put("sku_id", skuId);
                map.put("nums", nums);
                map.put("type",type);


                XUtil.Post(Url.LIJIXIADAN, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        Log.e("== 立即下单 ===", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            String info = jsonObject.optString("info");
                            if ("1".equals(res)) {

                                    Gson gson = new Gson();
                                OrderInfo.InfoBean infoBean = gson.fromJson(info, OrderInfo.InfoBean.class);

                                //添加购物车立即下单
                                startActivity(SettleAccounts2Activity.createIntent(AddCart2Activity.this, infoBean.getPackage_id(), infoBean.getSku_id(), infoBean.getNums(), infoBean.getPackage_name(), infoBean.getPackage_sku_info(), infoBean.getImg(),infoBean.getRemark(), infoBean.getPackage_money(), infoBean.getType(), infoBean.getWeight(), infoBean.getPackage_cate(), infoBean.getInstall_fee()));
                            } else {
                                MyToast(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished() {
                        closeDialog();
                        super.onFinished();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        closeDialog();
                    }
                });

                break;
        }
    }
}
